# coding=utf-8

import requests
import threading
import json
import time

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
}


# 请求ajax网页，得到json数据
def request(url):
    try:
        res = requests.get(url, headers=headers, timeout=10)
        j = res.json()
        return j
    except Exception as e:
        print(e)


# 获取每一个index的图片url列表,为url|itemTitle
def get(url):
    j = json.loads(request(url))
    imgs_list = []
    body = j.get('body')
    for i in body:
        if i.get('smallImg') and i.get('itemTitle'):
            imgs_list.append(i.get('smallImg') + '|' + i.get('itemTitle'))
    return imgs_list


def save_img(img_url):
    try:
        res = requests.get(img_url.split('|')[0], headers=headers)
        name = img_url.split("|")[-1]
        with open(file=r"E:\爬虫项目\游民星空壁纸爬取\%s.jpg" % name, mode='wb') as f:
            f.write(res.content)
            print(f"{name}爬取完成......")
    except Exception as e:
        print(e)


def run(page_url):
    imgs_list = get(page_url)
    tasks = [threading.Thread(target=save_img, args=(img_url,)) for img_url in imgs_list]
    for i in tasks:
        i.start()
    for i in tasks:
        i.join()


if __name__ == '__main__':
    start = time.time()
    for i in range(1, 21):
        url = "http://pic.gamersky.com/home/getimagesindex?sort=time_desc&pageIndex=%d&pageSize=50&nodeId=21092" % i
        run(url)
    end = time.time()
    print('爬取完毕，耗时：%.2fs' % (end - start))
